package ttk.muxiuesd.world;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.Disposable;
import ttk.muxiuesd.block.Block;
import ttk.muxiuesd.entity.EntityGroup;
import ttk.muxiuesd.entity.Player;
import ttk.muxiuesd.entity.enemy.Slime;
import ttk.muxiuesd.gameinterface.Drawable;
import ttk.muxiuesd.gameinterface.ShapeRenderable;
import ttk.muxiuesd.gameinterface.Updateable;
import ttk.muxiuesd.screen.MainGameScreen;
import ttk.muxiuesd.util.*;
import ttk.muxiuesd.world.chunk.ChunksManager;

/**
 * 主世界
 * */
public class MainWorld implements Disposable, Updateable, Drawable, ShapeRenderable {
    public static final String TAG = MainWorld.class.getName();


    public int worldSeed;

    private final MainGameScreen gameScreen;
    private ChunksManager chunksManager;
    private EntityGroup entityGroup;

    public MainWorld(MainGameScreen gameScreen) {
        this.gameScreen = gameScreen;

        this.entityGroup = new EntityGroup(this);
        new Player(this.entityGroup, 1000, 1000);
        new Slime(this.entityGroup, 1);
        new Slime(this.entityGroup, 1);
        new Slime(this.entityGroup, 1);

        this.worldSeed = (int)(Math.random() * 10000);
        Log.print(TAG, "世界种子为：" + this.worldSeed);
        this.chunksManager = new ChunksManager(this);
    }

    @Override
    public void update(float delta) {
        this.chunksManager.update(delta);
        this.entityGroup.update(delta);

        this.handleInput(delta);

    }

    @Override
    public void draw(Batch batch) {
        this.chunksManager.draw(batch);
        this.entityGroup.draw(batch);
    }

    @Override
    public void render(ShapeRenderer batch) {
        this.chunksManager.render(batch);
    }


    @Override
    public void dispose() {
        this.chunksManager.dispose();
        this.chunksManager = null;
        this.entityGroup.dispose();
        this.entityGroup = null;
    }

    /**
     * 处理输入
     * */
    private void handleInput (float delta) {
        Player player = this.entityGroup.player;
        Position playerCenter = player.getPlayerCenter();
        Block block = this.getBlock(playerCenter.x, playerCenter.y);
        player.curSpeed = player.speed * block.getProperty().getFriction();

        //C键控制区块边界是否绘制
        if (Gdx.input.isKeyJustPressed(Input.Keys.C)) {
            this.chunksManager.chunkEdgeRender = !this.chunksManager.chunkEdgeRender;
        }
        if (Gdx.input.isKeyJustPressed(Input.Keys.H)) {
            this.chunksManager.wallHitboxRender = !this.chunksManager.wallHitboxRender;
        }
        if (Gdx.input.isKeyJustPressed(Input.Keys.P)) {
            BlockPosition pbp = this.getPlayerBlockPosition();
            ChunkPosition pcp = this.getPlayerChunkPosition();

            Log.print(TAG, "玩家所在区块坐标：" + pcp.getX() + "," + pcp.getY());
            Log.print(TAG, "玩家所在方块坐标：" + pbp.getX() + "," + pbp.getY());
            Log.print(TAG, " 方块为：" + block.getClass().getName());
        }
    }



    public Block getBlock (float wx, float wy) {
        return this.chunksManager.getBlock(wx, wy);
    }

    /**获取玩家所在的方块坐标
     * */
    public BlockPosition getPlayerBlockPosition () {
        BlockPosition bp = new BlockPosition();
        Position playerCenter = this.entityGroup.player.getPlayerCenter();
        bp.setX((int) Util.fastRound(playerCenter.x));
        bp.setY((int)Util.fastRound(playerCenter.y));
        return bp;
    }

    /**
     * 获取玩家所在区块的坐标
     * @return
     */
    public ChunkPosition getPlayerChunkPosition () {
        return this.chunksManager.getPlayerChunkPosition();
    }

    public MainGameScreen getGameScreen() {
        return gameScreen;
    }

    public EntityGroup getEntityGroup() {
        return entityGroup;
    }
}
